using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;
using SolanumSolstice.Buildings;


namespace SolanumSolstice
{
    public class MiniMap
    {
        private Map map;
        private Vector2 size;
        private int startX;
        private int startY;
        private Texture2D texture;
        private Texture2D viewArea;
        private int scale;

        #region Accessors
        public int StartX
        {
            get
            {
                return startX;
            }
            set
            {
                startX = value;
            }
        }

        public int StartY
        {
            get
            {
                return startY;
            }
            set
            {
                startY = value;
            }
        }

        public Vector2 Size
        {
            get
            {
                return size;
            }
        }


        public Map Map
        {
            get
            {
                return map;
            }
            set
            {
                map = value;
            }
        }

        public int ScaleSize
        {
            get
            {
                return scale;
            }
            set
            {
                scale = value;
            }
        }
        #endregion

        public MiniMap(Map map, Texture2D texture, Texture2D viewArea, int scale)
        {
            this.scale = scale;
            this.map = map;
            size = new Vector2(Scale(map.MapSizeX), Scale(map.MapSizeY));
            startX = (int)map.ScreenSizeX - (int)size.X - 20;
            startY = (int)map.ScreenSizeY + map.HUDHeight - (int)size.Y - 20;
            
            this.texture = texture;
            this.viewArea = viewArea;
        }

        public Vector2 gridToPixels(Vector2 grid) { return new Vector2(grid.X * 32, grid.Y * 32); }

        public Vector2 pixelsToGrid(Vector2 pixels) { return new Vector2(pixels.X / 32, pixels.Y / 32); }

        public void drawBuildingDot(SpriteBatch sb, Building bild, int sz, Color color)
        {
            sb.Draw(texture, new Rectangle(startX + Scale((int)bild.Position.X), startY + Scale((int)bild.Position.Y), scale * sz, scale * sz), color);
        }

        public void Draw(SpriteBatch spritebatch, List<Human> units)
        {


            for (int x = 0; x < map.Tiles.GetLength(0); x++)
            {
                for (int y = 0; y < map.Tiles.GetLength(1); y++)
                {
                    Color color = Color.Yellow;

                    if (map.Tiles[x,y].IsWalkable == false)
                    {
                        color = Color.Green;
                    }

                    if (map.Tiles[x, y].IsVisible == false)
                    {
                        color = new Color(new Vector4(0, 0, 0, 0.85f));
                    }

                    spritebatch.Draw(texture, new Rectangle(startX + scale * x, startY + scale * y, scale, scale), color);
                }
            }

            foreach (Human unit in units)
            {
                spritebatch.Draw(texture, new Rectangle(startX + Scale((int)unit.Position.X), startY + Scale((int)unit.Position.Y), scale * 2, scale * 2), Color.Red);
            }

            foreach (Zombie robzombie in SolanumSolstice.activeZombies)
            {
                Vector2 gridloc = pixelsToGrid(robzombie.Position);
                if ( (SolanumSolstice.detectZombiesUnlocked==true)||((SolanumSolstice.detectZombiesUnlocked==false)&&(map.Tiles[(int)gridloc.X,(int)gridloc.Y].IsVisible)) ) {
                   spritebatch.Draw(texture, new Rectangle(startX + Scale((int)robzombie.Position.X), startY + Scale((int)robzombie.Position.Y), scale * 2, scale * 2), Color.Purple);
                }
            }

            Color bcolor = Color.Blue;

            drawBuildingDot(spritebatch, SolanumSolstice.scibase, 4, bcolor);
            drawBuildingDot(spritebatch, SolanumSolstice.milbase, 3, bcolor);
            drawBuildingDot(spritebatch, SolanumSolstice.beacon, 3, bcolor);
            drawBuildingDot(spritebatch, SolanumSolstice.woodbase, 4, bcolor);
            drawBuildingDot(spritebatch, SolanumSolstice.foodbase, 3, bcolor);
            drawBuildingDot(spritebatch, SolanumSolstice.mainbase, 4, bcolor);
            //drawBuildingDot(spriteBatch, SolanumSolstice.mainbase, bcolor);

            
            //    spritebatch.Draw(texture, new Rectangle(startX + Scale((int)SolanumSolstice.scibase.Position.X), startY + Scale((int)Solan.Position.Y), scale * 2, scale * 2), bcolor);

            

            /*foreach (Building bild in SolanumSolstice.buildings)
            {
                Vector2 gridloc = pixelsToGrid(bild.Position);
                if ( (bild.BuildingType != BuildingType.None) && (bild.BuildingType != BuildingType.Spawn) && (map.Tiles[(int)gridloc.X, (int)gridloc.Y].IsVisible))
                {
                    Color color = Color.Green;
                    if (bild.IsInfested) color = Color.Orange;
                    spritebatch.Draw(texture, new Rectangle(startX + Scale((int)bild.Position.X), startY + Scale((int)bild.Position.Y), scale * 2, scale * 2), color);
                }
            }*/

 
    
                spritebatch.Draw(viewArea,new Rectangle(startX + Scale(map.FocusX), startY + Scale(map.FocusY), Scale(map.ScreenSizeX), Scale(map.ScreenSizeY)), Color.White);
                       

        }

        public int Scale(int scalable)
        {
            return (scalable / (Tile.TILEWIDTH / scale));
        }

    }
}
